<?php
namespace M\Controller;
use Think\Controller;
class NewsController extends CommonController {

    //资讯详情
    public function details(){

            $id = I('get.id');
            if(!empty($id)){

                    $map['id'] = $id;
            }else{

                    $this->error('参数错误');
            }

            $User = M('news');
            $get_new = $User->where($map)->find();
            $get_new['content'] = htmlspecialchars_decode($get_new['content']);
            
            $get_new['ifcollection'] = $this->_chk_if_member_follow_founding($this->memberinfo['id'], $id, 2);
            
            //热门评论
            $get_new['comment_count']=  $this->_get_one_comments_count($id,2);
            $get_new['hot_comments']=  $this->_get_one_hot_comments($id,2);
//             print_r($get_new); die;

            $this->assign('get_new',$get_new);
            $this->assign('mid', $this->memberinfo['id']);
            $this->assign('id', $id);
            
            $this->display(); 
    }

    //APP来源访问

    public function details_for_app(){

            $id = I('get.newsid');
            if(!empty($id)){

                    $map['id'] = $id;
            }else{

                    $this->error('参数错误');
            }

            $User = M('news');
            $get_new = $User->where($map)->find();
            $get_new['content'] = htmlspecialchars_decode($get_new['content']);
            // print_r($get_new); die;
            $this->assign('get_new',$get_new);
            $this->display(); 
    }

    /**
     * 获取危害须知的详情页面
     */
    public function harm_details_for_app(){

            $id = I('get.newsid');
            if(!empty($id)){

                    $map['id'] = $id;
            }else{

                    $this->error('参数错误');
            }

            $User = M('news');
            $get_new = $User->where($map)->find();
            $get_new['content'] = htmlspecialchars_decode($get_new['content']);
            // print_r($get_new); die;
            $this->assign('get_new',$get_new);
            $this->display(); 
    }

    /**
     * 获取食事周刊的详情页面
     */
    public function weekly_details_for_app(){

        $id = I('get.newsid');
            if(!empty($id)){

                    $map['id'] = $id;
            }else{

                    $this->error('参数错误');
            }

            $User = M('news');
            $get_new = $User->where($map)->find();
            $get_new['content'] = htmlspecialchars_decode($get_new['content']);
            // print_r($get_new); die;
            $this->assign('get_new',$get_new);
            $this->display(); 
    }
        
    //分享的内容添加尾巴

    public function details_for_share(){
        /*
         * Date: 2015-12-16
         * 增加一个判断是否是关注用户
         * 
         */
        $openid = session('openid');
        
        $DB_openid = M("members_subscribe_records");
        $map_openid['openid']=$openid;
        $map_openid['issubscribe']=1;
        $res_openid = $DB_openid->where($map_openid)->find();
        if(empty($res_openid))
        {
            $this->assign('issubscribe', 0);
        }
        else
        {
            $this->assign('issubscribe', 1);
        }

            $id = I('get.id');
            if(!empty($id)){

                    $map['id'] = $id;
            }else{

                    $this->error('参数错误');
            }

            $User = M('news');
            $get_new = $User->where($map)->find();
            $get_new['content'] = htmlspecialchars_decode($get_new['content']);
            // print_r($get_new); die;
            
            $get_new['ifcollection'] = $this->_chk_if_member_follow_founding($this->memberinfo['id'], $id, 2);
            
            //热门评论
            $get_new['comment_count']=  $this->_get_one_comments_count($id,2);
            $get_new['hot_comments']=  $this->_get_one_hot_comments($id,2);
            
            $this->assign('get_new',$get_new);
            $this->assign('mid', $this->memberinfo['id']);
            $this->assign('id', $id);
            $this->display(); 
    }
        
        
    /*
     * 
     * 文章的评论列表
     * 
     */
    public function comments() {
        $fdid = I('get.fdid');
        $pid = I('get.pid');
        
        $map['pid']=$pid;
        $map['ctype']=2;
        $map['for_id']=$fdid;
        $map['ishidden']=0;
        
        $DB = M("comments");
        $res = $DB->where($map)->order('ctime DESC')->select();
        
        foreach ($res as $key => $value) {
            $res[$key]['username']=$this->_get_member_nickname($value['mid']);
            $res[$key]['avatar']=  $this->_get_one_avatar($value['mid']);
        }
        
        // print_r($res); die;
        $this->assign('result', $res);
        $this->assign('id', $fdid);
        
        $this->display();
    }


    /**
     * 评论详情页面
     */
    public function comment_detail(){

        if(IS_GET){
//            $fdid = I('get.fdid');
            $pid = I('get.pid');
            $fdid = I("get.fdid");
            $DB = M("comments");
            //父评论信息
            $pcomment = $DB->find($pid);
            $pcomment['username']=$this->_get_member_nickname($pcomment['mid']);
            $pcomment['avatar']=  $this->_get_one_avatar($pcomment['mid']);
            
            //众多的子评论信息
            $map['pid']=$pid;
            $map['ctype']=2;
            $map['for_id']=$fdid;
            $map['ishidden']=0;
            
            
            $res = $DB->where($map)->order("ctime DESC")->select();
            foreach ($res as $key => $value) {
                $res[$key]['username']=$this->_get_member_nickname($value['mid']);
                $res[$key]['avatar']=  $this->_get_one_avatar($value['mid']);
            }
            // print_r($res); die;
            $this->assign('pcomment', $pcomment);
            $this->assign('result', $res);
            $this->assign('mid',  session('mid'));
//            print_r($this->memberinfo);
//            exit(session('mid'));
            $this->assign('fdid', $fdid);
            $this->assign('pid', $pid);
            $this->display();
            
//            print_r($pcomment);
        }
    }

            /*
     * 获取单个用户的头像信息
     */
    protected function _get_one_avatar($mid) {
        $member = $this->_get_member_info($mid);
        return $member['avatar'];
    }


    /*
     * 获取单个用户的昵称信息
     */
    protected function _get_member_nickname($mid)
    {
        $member = $this->_get_member_info($mid);
        $member_name=  $member['username'];
        if(mb_strlen($member_name)>8)
        {
            $member_name = mb_substr($member_name, 0,8,'utf-8');
        }
        return $member_name;
    }
    
    /*
     * 
     * 文章的评论添加
     * 调用widget 里边的函数
     * 
     */
    public function comments_add() {
//        comment_add($pid=0,$mid=0,$content="",$ctype=0,$for_id=0)
        $pid = I('post.pid');
        $mid = $this->memberinfo['id'];
        $content = I("post.content");
        $for_id = I("post.fdid");

        $chk_back = chk_bad_words($content,1);

        if($chk_back){

            echo json_encode(2);
        }else{
            
            $res = R("Widget/comment_add",array('pid'=>$pid,'mid'=>$mid,'content'=>"$content",'ctype'=>2,'for_id'=>$for_id));
            if($res)
            {
                /*
                * 增加用户的健康豆
                */
               $this->member_action_bonus('comment', $mid, 1);
               
               /*
                * 新闻的权重加一
                * 
                */
               
               M("news")->where("id=".$for_id)->setInc("comment_count");
               
               send_msg_to_goal_user($res);
                echo 1;
            }
            else
            {
                echo 0;
            }
        }
    }
    
    /*
     * 
     * 文章的评论点赞
     * 
     */
    public function comments_like_add() {
        $mid = $this->memberinfo['id'];

        if($mid)
        {
            $cid = I("get.cid");

            $data['cid']=$cid;
            $data['mid']=$mid;
            $data['ctime']=I("server.REQUEST_TIME");
            
            $DB = M("comment_likes");
            
            $id = $DB->add($data);
            
            if($id)
            {
                $DB_comment = M("comments");
                $DB_comment->where("id=".$cid)->setInc("likecount");
//                print_r($DB_comment->getLastSql());
                
                echo 1;
            }
        }
        else
        {
            echo 0;
        }
    }
    
    /*
     * 针对文章的收藏功能
     */
    
    public function add_one_colletion() {
       /*
        * 收藏or 取消收藏
        * 0 取消收藏
        * 1 收藏
        * 
        * ctype 1 项目, 2 资讯，
        */
        $mid = I('get.mid');
        
        if($mid)
        {
            $ctype = 2;
            $forid = I("get.id");
            
            $ifcollection = I("get.ifcollection");
            
            $DB = M("collections");
       
            if($ifcollection==1)
            {
                $map['ctype']=$ctype;
                $map['for_id']=$forid;
                $map['mid']=$mid;

                $DB->where($map)->delete();
                
                /*
                * 新闻的权重加一
                * 
                */

               M("news")->where("id=".$forid)->setDec("collection_count");
                
                 echo 1;
            }
            else
            {
                //新增
                $data['ctype']=$ctype;
                $data['for_id']=$forid;
                $data['mid']=$mid;
                $data['ctime']=I("server.REQUEST_TIME");


                $id =$DB->add($data,array(),true);
                if($id)
                {
                    /*
                    * 新闻的权重加一
                    * 
                    */
                    M("news")->where("id=".$forid)->setInc("collection_count");
                    echo 1;
                }

            }
           
        }
        else
        {
            echo 0;
        }
        
        
       
   }
   
   /*
    * 新的详情模板（评论部分）
    */
   public function news_details_template() {
       $this->display();
   }
   
   /*
    * 新的评论模板
    */
   public function news_comment_template() {
       $this->display();
   }
   
   /*
    * 新的评论详情模板
    */
   public function news_comment_detail_template() {
       $this->display();
   }
    
        
}
?>